#include "BH1750FVI.h"
#include  <INTRINS.H>
#include <STC15F2K60S2.H>
#include "delay.h"

#define uchar unsigned char
#define uint  unsigned int
#define	  SlaveAddress   0x46 
#define  IIC_WRITE      0                 // WRITE direction bit
#define  IIC_READ       1  

typedef   unsigned char BYTE;
typedef   unsigned short WORD;
uchar  BUF[5];

sbit	  SCL=P1^5;      
sbit  	  SDA=P1^6;

/**
 * @brief start DS1302
 * 
 */
void BH1750_Start()
{
    SDA = 1;                    //拉高数据线
    SCL = 1;                    //拉高时钟线
    delay_us(5);                 //延时
    SDA = 0;                    //产生下降沿
    delay_us(5);                 //延时
    SCL = 0;                    //拉低时钟线
}

/**
 * @brief Stop signal
 * 
 */
void BH1750_Stop()
{
    SDA = 0;                    //拉低数据线
    SCL = 1;                    //拉高时钟线
    delay_us(5);                 //延时
    SDA = 1;                    //产生上升沿
    delay_us(5);                 //延时
}

/**
 * @brief Send response signal
 * 
 * @param ack 
 */
void BH1750_SendACK(bit ack)
{
    SDA = ack;                  //写应答信号
    SCL = 1;                    //拉高时钟线
    delay_us(5);                 //延时
    SCL = 0;                    //À拉低时钟线
    delay_us(5);                 //延时
}

/**
 * @brief Receive response signal
 * 
 * @return bit 
 */
bit BH1750_RecvACK()
{
    SCL = 1;                    //拉高时钟线
    delay_us(5);                 //延时
    CY = SDA;                   //读应答信号
    SCL = 0;                    //拉低时钟线
    delay_us(5);                 //延时

    return CY;
}

/**
 * @brief Send a byte of data to the IIC bus
 * 
 * @param dat 
 */
void BH1750_SendByte(unsigned char dat)
{
    unsigned char i;

    for (i=0; i<8; i++)         //8位计数器
    {
        dat <<= 1;              //移出数据的最高位
        SDA = CY;               //送数据口
        SCL = 1;                //拉高时钟线
        delay_us(5);             //延时
        SCL = 0;                //拉低时钟线
        delay_us(5);             //延时
    }
    BH1750_RecvACK();
}

/**
 * @brief Receive a byte of data from the IIC bus
 * 
 * @return dat
 */
unsigned char BH1750_RecvByte()
{
    BYTE i;
    BYTE dat = 0;

    SDA = 1;                    //使能内部上拉,准备读取数据,
    for (i = 0; i < 8; i++)         //8位计数器
    {
        dat <<= 1;
        SCL = 1;                //拉高时钟线
        delay_us(5);             //延时
        dat |= SDA;             //读数据               
        SCL = 0;                //拉低时钟线
        delay_us(5);             //延时
    }
    return dat;
}

//*********************************

void Single_Write_BH1750(unsigned char REG_Address)
{
    BH1750_Start();                  //起始信号
    BH1750_SendByte(SlaveAddress);   //发送设备地址+写信号
    BH1750_SendByte(REG_Address);    //内部寄存器地址，请参考中文pdf22页 
  //  BH1750_SendByte(REG_data);       //内部寄存器数据，请参考中文pdf22页 
    BH1750_Stop();                   //发送停止信号
}


/**
 * @brief Continuous reading of BH1750 internal data
 * 
 */
void Multiple_read_BH1750(void)
{   unsigned char i;	
    BH1750_Start();                          //起始信号
    BH1750_SendByte(SlaveAddress+1);         //发送设备地址+读信号
	
	 for (i = 0; i < 3; i++)                      //连续读取6个地址数据，存储中BUF
    {
        BUF[i] = BH1750_RecvByte();          //BUF[0]存储0x32地址中的数据
        if (i == 3)
        {

           BH1750_SendACK(1);                //最后一个数据需要回NOACK
        }
        else
        {		
          BH1750_SendACK(0);                //回应ACK
        }
   }

    BH1750_Stop();                          //停止信号
    delay_ms(5);
}

/**
 * @brief BH1750FVI getdata
 * 
 * @return int temp
 */
int BH1750FVI_get_data()
{   
    uint  dis_data;
    float temp;

    Single_Write_BH1750(0x01);   // power on
    Single_Write_BH1750(0x10);   // H- resolution mode
    delay_ms(180);               //延时180ms
    Multiple_read_BH1750();      //连续读出数据，存储在BUF中  
    dis_data=BUF[0];
    dis_data=(dis_data<<8)+BUF[1];//合成数据 
    temp=(float)dis_data/1.2;
    return temp;
}